package com.kchouse;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kchouse.common.BaseFragment;
import com.kchouse.common.ToastUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * A simple {@link Fragment} subclass.
 */
public class InquiryFragment extends BaseFragment implements View.OnClickListener {


    private View view;
    private ListView lv_myproblem;
    private LayoutInflater inflater;
    private TextView tv_sub;
    private boolean[] itemcb;
    private String[] itemcbstr;
    public int num = 0;
    private HashMap<Integer, String> hashMap = new HashMap<>();

    private ArrayList<String> list = new ArrayList<>();
    private Intent intent = new Intent();
    private View lay_customer;
    private View lay_owner;
    private ListView lv_myanswer;
    private boolean isLogin = false;//是否登录

    public InquiryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (view == null) {
            this.inflater = inflater;
            view = inflater.inflate(R.layout.fragment_inquiry, null);
            intiView();
            intiData();
        }

        return view;
    }

    private void intiData() {
        list.add("集美大众有限公司");
        list.add("集美大众有限公司2");
        list.add("集美大众有限公司3");
        list.add("集美大众有限公司4");
        list.add("集美大众有限公司5");
        list.add("集美大众有限公司6");
        list.add("集美大众有限公司7");
        list.add("集美大众有限公司5");
        list.add("集美大众有限公司6");
        list.add("集美大众有限公司7");
        list.add("集美大众有限公司8");
        list.add("集美大众有限公司9");
        list.add("集美大众有限公司10");
        list.add("集美大众有限公司11");
        list.add("集美大众有限公司12");
        list.add("集美大众有限公司13");
        list.add("集美大众有限公司14");
        list.add("集美大众有限公司15");
        list.add("集美大众有限公司16");
    }

    private void intiView() {
        lay_customer = view.findViewById(R.id.lay_customer);//客户界面
        lay_owner = view.findViewById(R.id.lay_owner);//服务商界面
        lv_myproblem = (ListView) view.findViewById(R.id.lv_myproblem);
        lv_myproblem.setAdapter(new myproblemAdapter());
        lv_myanswer = (ListView) view.findViewById(R.id.lv_myanswer);
        lv_myanswer.setAdapter(new myanswerAdapter());

        tv_sub = (TextView) view.findViewById(R.id.tv_sub);
        tv_sub.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_sub:
                if (!isLogin) {
                    checkidentity();
                } else {
                    inticbitem();
                    subproblem();
                }
                break;

        }
    }

    /**
     * 确定身份（登录接口未通）
     */
    private void checkidentity() {
        // 创建构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // 设置参数
        builder.setTitle("是否是服务商家").setPositiveButton("是", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog,
                                int which) {
//                        ToastUtils.getInstanc(getActivity()).showToast("您是服务商家不能回答");

//                        intent.setClass(getActivity(),ProblemListActivity.class);
//                        startActivity(intent);

                lay_customer.setVisibility(View.GONE);
                lay_owner.setVisibility(View.VISIBLE);

                isLogin = true;

            }
        }).setNegativeButton("不是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                lay_customer.setVisibility(View.VISIBLE);
                lay_owner.setVisibility(View.GONE);
                isLogin = true;
            }
        });
        builder.create().show();
    }



    /**
     * 提交问题
     */
    private void subproblem() {
        AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("服务商家")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                            field.setAccessible(true);
                            field.set(dialog,
                                    true); // true - 使之可以关闭(此为机关所在，其它语句相同)
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {


                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        num = 0;
                        for (int i = 0; i < itemcb.length; i++) {
                            Log.e("====", "itemcb:" + itemcb[i]);

                            if (itemcb[i]) {
                                num++;
                            }
                        }

                        if (num < 6) {
                            ToastUtils.getInstanc(getActivity()).showToast("发送成功");
                            // 条件成立能关闭 AlertDialog 窗口
                            try {
                                Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                field.setAccessible(true);
                                field.set(dialog,
                                        true); // true - 使之可以关闭(此为机关所在，其它语句相同)
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            ToastUtils.getInstanc(getActivity()).showToast("不可超过5人");
                            // 条件不成立不能关闭 AlertDialog 窗口
                            try {
                                Field field = dialog.getClass().getSuperclass().getDeclaredField("mShowing");
                                field.setAccessible(true);
                                field.set(dialog,
                                        false); // false - 使之不能关闭(此为机关所在，其它语句相同)
                            } catch (Exception e) {
//                                            Log.e( e.getMessage() );
                                e.printStackTrace();
                            }
                        }
                    }
                })

                .setMultiChoiceItems(itemcbstr, itemcb, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        itemcb[which] = isChecked;
                    }
                }).create();
        dialog.show();
    }

    /**
     * 初始化多选item
     */
    private void inticbitem() {
        itemcb = new boolean[list.size()];
        itemcbstr = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            itemcb[i] = false;
            itemcbstr[i] = list.get(i);
        }
    }

    /**
     * 我的回答适配器
     */
    class myanswerAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 8;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View lvview = inflater.inflate(R.layout.lv_myanswer_item, null);
            EditText et_answer = (EditText) lvview.findViewById(R.id.et_answer);

            et_answer.addTextChangedListener(new TextSwitcher(position));
            et_answer.setText(hashMap.get(position));
            return lvview;
        }
    }

    class TextSwitcher implements TextWatcher {

        private int position;

        public TextSwitcher(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
//            int position = (int) mHolder.editText.getTag();//取tag值
//            ((MainActivity)context).saveEditData(position, s.toString());
            hashMap.put(position, "" + s);

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    /**
     * 我的问题适配器
     */
    class myproblemAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 8;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View lvview = inflater.inflate(R.layout.lv_myproblem_item, null);
            return lvview;
        }
    }

}
